package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.PurchaseRecords;
import com.ruoyi.system.domain.SparePartsInventory;
import com.ruoyi.system.mapper.PurchaseRecordsMapper;
import com.ruoyi.system.service.IPurchaseRecordsService;
import com.ruoyi.system.service.ISparePartsInventoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 采购记录管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class PurchaseRecordsServiceImpl implements IPurchaseRecordsService {
    @Autowired
    private PurchaseRecordsMapper purchaseRecordsMapper;
    @Autowired
    private ISparePartsInventoryService iSparePartsInventoryService;

    /**
     * 查询采购记录管理
     *
     * @param recordId 采购记录管理主键
     * @return 采购记录管理
     */
    @Override

    public PurchaseRecords selectPurchaseRecordsByRecordId(Long recordId) {
        return purchaseRecordsMapper.selectPurchaseRecordsByRecordId(recordId);
    }

    /**
     * 查询采购记录管理列表
     *
     * @param purchaseRecords 采购记录管理
     * @return 采购记录管理
     */
    @Override
    public List<PurchaseRecords> selectPurchaseRecordsList(PurchaseRecords purchaseRecords) {
        return purchaseRecordsMapper.selectPurchaseRecordsList(purchaseRecords);
    }

    /**
     * 新增采购记录管理
     *
     * @param purchaseRecords 采购记录管理
     * @return 结果
     */
    @Override
    public int insertPurchaseRecords(PurchaseRecords purchaseRecords) {
        purchaseRecords.setCreateTime(DateUtils.getNowDate());
        purchaseRecords.setPurchaserName(SecurityUtils.getLoginUser().getUser().getNickName());
        purchaseRecords.setPurchaseTime(new Date());
        purchaseRecordsMapper.insertPurchaseRecords(purchaseRecords);
        SparePartsInventory sparePartsInventory = new SparePartsInventory();
        BeanUtils.copyProperties(purchaseRecords, sparePartsInventory);
        sparePartsInventory.setPartName(purchaseRecords.getSparePartName());
        sparePartsInventory.setStockQuantity(purchaseRecords.getQuantity());
        iSparePartsInventoryService.insertSparePartsInventory(sparePartsInventory);
        return 1;
    }

    /**
     * 修改采购记录管理
     *
     * @param purchaseRecords 采购记录管理
     * @return 结果
     */
    @Override
    public int updatePurchaseRecords(PurchaseRecords purchaseRecords) {
        purchaseRecords.setUpdateTime(DateUtils.getNowDate());
        return purchaseRecordsMapper.updatePurchaseRecords(purchaseRecords);
    }

    /**
     * 批量删除采购记录管理
     *
     * @param recordIds 需要删除的采购记录管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseRecordsByRecordIds(Long[] recordIds) {
        return purchaseRecordsMapper.deletePurchaseRecordsByRecordIds(recordIds);
    }

    /**
     * 删除采购记录管理信息
     *
     * @param recordId 采购记录管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseRecordsByRecordId(Long recordId) {
        return purchaseRecordsMapper.deletePurchaseRecordsByRecordId(recordId);
    }
}
